package com.tanhua.server.controller;


import com.tanhua.model.domain.UserInfo;
import com.tanhua.server.exception.BusinessException;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserInfoService;
import com.tanhua.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.tanhua.model.vo.ErrorResult;

import java.io.IOException;
import java.util.Map;

/**
 * @Author: cl
 * @Date: 2022/4/29 - 04 - 29
 * @Description:
 */
@RestController
@RequestMapping(path = "/user")
public class UserController {

    //用户登录
    @Autowired
    private UserService userService;

    //用户信息
    @Autowired
    private UserInfoService userInfoService;


    /**
     * 用户登录(发送验证码)
     *
     * @param map
     * @return
     */
    @PostMapping("login")
    public ResponseEntity<String> login(@RequestBody Map<String, String> map) {

        //1、获取手机号
        //String phone = user.getMobile();
        String phone = map.get("phone");

//        try {
            //2、发送验证码
            userService.sendCode(phone);
//        } catch (Exception e) {
//            //验证码发送失败，抛出自定义异常
//            throw new BusinessException(ErrorResult.fail());
//        }

        //3、返回数据
        return ResponseEntity.ok("验证码发送成功！");
//        return ResponseEntity.status(404).body("没有找到网页！");

    }

    /**
     * 用户登录(校验登录)
     *
     * @param map
     * @return
     */
    @PostMapping("/loginVerification")
    public ResponseEntity loginVerification(@RequestBody Map<String, String> map) {
        //1、获取前端传过来的数据
        String phone = map.get("phone");
        String code = map.get("verificationCode");

        //2、调用service进行逻辑分析
        Map retMap = userService.loginVerification(phone, code);

        //3、返回前端结果
        return ResponseEntity.ok(retMap);
    }

    /**
     * 保存用户信息
     *
     * @param userInfo
     * @return
     */
    @PostMapping("/loginReginfo")
    public ResponseEntity loginReginfo(@RequestBody UserInfo userInfo) {

//        //1、判断token是否合法(验证用户是否登录)
//        boolean verifyToken = JwtUtils.verifyToken(token);
//        if (!verifyToken) {
//            return ResponseEntity.status(401).body("用户未登录");
//        }
//
//        //2、向userinfo中设置用户的Id(token中保存了用户id)
//        Claims claims = JwtUtils.getClaims(token);
//        Integer id = (Integer) claims.get("id");


        //线程中获取id
        Long userId = UserHolder.getUserId();
        userInfo.setId(Long.valueOf(userId));

        //3、调用service完成逻辑分析
        userInfoService.save(userInfo);
        return ResponseEntity.ok("信息保存成功");
    }


    /**
     * 修改用户信息(更新头像)
     *
     * @param headPhoto
     * @return
     */
    @PostMapping("/loginReginfo/head")
    public ResponseEntity head(MultipartFile headPhoto) throws IOException {

//        //1、判读用户是否登录
//        boolean b = JwtUtils.verifyToken(token);
//        if (!b) {
//            return ResponseEntity.status(401).body("用户未登录");
//        }
//
//        //2、获取用户id用于封装并传递给service处理
//        Claims claims = JwtUtils.getClaims(token);
//        Integer id = (Integer) claims.get("id");

        Long userId = UserHolder.getUserId();

        userInfoService.updateHead(headPhoto, userId);

        return ResponseEntity.ok("上传头像成功");
    }

}
